﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Carriage
{
    public class EMSClass
    {
        private double _weight = 0;
        /// <summary>
        /// 货物净重(单位:克) 
        /// </summary>
        internal double weight
        {
            get
            {
                return _weight;
            }
            set
            {
                _weight = value;
            }
        }

        private double _weightrate = 1;
        /// <summary>
        /// 毛重与净重的比率
        /// </summary>
        public double weightrate
        {
            get
            {
                return _weightrate;
            }
            set
            {
                _weightrate = value;
            }
        }

        private string _CountryName;
        /// <summary>
        /// 国家名称
        /// </summary>
        public string CountryName
        {
            get
            {
                return _CountryName;
            }
            set
            {
                _CountryName = value;
            }
        }

        private EMSType _emsType = EMSType.international;
        /// <summary>
        /// EMS类型
        /// </summary>
        public EMSType emsType
        {
            get
            {
                return _emsType;
            }
            set
            {
                _emsType = value;
            }
        }

        private EMSAreas _emsArea = EMSAreas.Area1;
        /// <summary>
        /// EMS区域
        /// </summary>
        public EMSAreas emsArea
        {
            get
            {
                return _emsArea;
            }
            set
            {
                _emsArea = value;
            }
        }

        /// <summary>
        /// EMS类型
        /// </summary>
        public enum EMSType
        {
            /// <summary>
            /// 国内
            /// </summary>
            Chinese_Mainland = 0,
            /// <summary>
            /// 国际及港、澳、台
            /// </summary>
            international = 1,
        }

        /// <summary>
        /// EMS区域
        /// </summary>
        public enum EMSAreas
        {
            /// <summary>
            /// 未知
            /// </summary>
            UnKnow = 0,
            /// <summary>
            /// 一区
            /// </summary>
            Area1 = 1,
            /// <summary>
            /// 二区
            /// </summary>
            Area2 = 2,
            /// <summary>
            /// 三区
            /// </summary>
            Area3 = 3,
            /// <summary>
            /// 四区
            /// </summary>
            Area4 = 4,
            /// <summary>
            /// 五区
            /// </summary>
            Area5 = 5,
            /// <summary>
            /// 六区
            /// </summary>
            Area6 = 6,
            /// <summary>
            /// 七区
            /// </summary>
            Area7 = 7,
            /// <summary>
            /// 八区
            /// </summary>
            Area8 = 8,
            /// <summary>
            /// 九区
            /// </summary>
            Area9 = 9,
            /// <summary>
            /// 十区
            /// </summary>
            Area10 = 10
        }

        private EMSAreas GetAreaByName(string country)
        {
            string filePath = System.Web.HttpContext.Current.Server.MapPath("~/Countrys.xml");
            System.IO.FileInfo fi = new System.IO.FileInfo(filePath);
            if (fi.Exists)
            {
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(filePath);
                foreach (System.Xml.XmlNode node in doc.SelectNodes("Root//Country"))
	            {
                    if (country.ToLower() == node.SelectSingleNode("Name").InnerText.ToLower())
                    {
                        return (EMSAreas)(Convert.ToInt32(node.SelectSingleNode("EmsArea").InnerText));
                    }
	            }
                return EMSAreas.UnKnow;
            }
            else
            {
                throw new Exception("the file countrys.xml is not exist");
            }
        }

        internal double GetCarriage()
        {
            double rlt = 0;
            double value1 = 0;//起重标准
            double value2 = 0;//续重标准
            double cw = this.weight * this.weightrate;//货物的毛重
            this.emsArea = this.GetAreaByName(this.CountryName);
            switch (this.emsType)
            {
                case EMSType.Chinese_Mainland:
                    value1 = 20;
                    switch (this.emsArea)
                    {
                        case EMSAreas.Area1:
                            value2 = 6;
                            break;
                        case EMSAreas.Area2:
                            value2 = 9;
                            break;
                        case EMSAreas.Area3:
                            value2 = 15;
                            break;
                        default:
                            throw new Exception("EMS国内区域超出范围");
                            break;
                    }
                    break;
                case EMSType.international:
                    switch (this.emsArea)
                    {
                        case EMSAreas.Area1:
                            value1 = 150;
                            value2 = 30;
                            break;
                        case EMSAreas.Area2:
                            value1 = 180;
                            value2 = 40;
                            break;
                        case EMSAreas.Area3:
                            value1 = 190;
                            value2 = 45;
                            break;
                        case EMSAreas.Area4:
                            value1 = 210;
                            value2 = 55;
                            break;
                        case EMSAreas.Area5:
                            value1 = 280;
                            value2 = 75;
                            break;
                        case EMSAreas.Area6:
                            value1 = 240;
                            value2 = 75;
                            break;
                        case EMSAreas.Area7:
                            value1 = 325;
                            value2 = 90;
                            break;
                        case EMSAreas.Area8:
                            value1 = 335;
                            value2 = 100;
                            break;
                        case EMSAreas.Area9:
                            value1 = 445;
                            value2 = 120;
                            break;
                        case EMSAreas.Area10:
                            value1 = 455;
                            value2 = 120;
                            break;
                        default:
                            throw new Exception("EMS 未知区域");
                            break;
                    }
                    break;
                default:
                    break;
            }
            if (!(cw > 0))
            {
                throw new Exception("错误的重量");
            }
            else if (cw <= 500)
            {
                rlt = value1;
            }
            else
            {
                rlt = value1 + ((cw - 500) / 500) * value2;
            }
            return rlt;
        }
    }
}
